<?php
/**
 * Created by PhpStorm.
 * User: UGLY
 * Date: 2017/12/26
 * Time: 18:13
 */

namespace app\mainiu\controller;

use think\Db;
use app\blog\controller\CommonController;
use think\Validate;

class CodeController extends CommonController
{
    //二维码管理主页
    public function codeindex()
    {
        echo 123;
    }
    //二维码查看
    public function codewatch()
    {
        return $this->fetch();
    }
    //返回所有二维码数据
    public function returncode()
    {
        //管理员id
        $id = $_SESSION['think']['ADMIN_ID'];
        $data = Db::name('qrcode')->where(['aid'=>$id])->order('id desc')->select();
        if($data){
            echo json_encode($data);
        }else{
            echo 2;
        }
    }
    //二维码添加页面
    public function codeadd()
    {
        return $this->fetch();
    }
    //二维码添加方法
    public function add()
    {
        $request = request();
        $post = $request->post();

        //要验证的字段
        $rule = [
            'name' => 'require|max:10',
        ];
        $thumb = 'http://'.$_SERVER['HTTP_HOST'].'/two_code/'.$post['name'].'.png';
        //获取提交过来的url地址
        $url = $post['url'];
        if($url == ''){
            $url = 'https://www.baidu.com/';
        }

        //调用生成二维码
        vendor('phpqrcode.phpqrcode');
        //容错级别
        $errorCorrectionLevel = intval(3);
        //生成图片大小
        $matrixPointSize = intval(15);
        //生成二维码图片
        $object = new \QRcode();
        //第二个参数false的意思是不生成图片文件，如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件
        $object->png($url, './two_code/'.$post['name'].'.png', $errorCorrectionLevel, $matrixPointSize, 2);


        $data = [
            'name'=>$post['name'],
            'des'=>$post['des'],
            'aid'=>$_SESSION['think']['ADMIN_ID'],
            'thumb'=>$thumb,
            'url'=>$url,
            'time'=>date("Y-m-d H:i:s",time())
        ];
        //出错的信息
        $msg = [
            'name.require' => '请填写您的名称',
            'name.max'     => '名称最多不能超过10个字符',
        ];
        $validate = new Validate($rule,$msg);
        if($validate->check($data)){
            if(Db::name('qrcode')->insert($data)){
                $data = [
                  'sta'=>1,
                  'codethumb'=>$thumb,
                ];
                echo json_encode($data);
            }else{
                echo 2;
            }
        }else{
            echo json_encode($validate->getError());
        }
    }
    //二维码删除
    public function del()
    {
        $post = request();
        $post = $post->post();
        $name = \db('qrcode')->where(['id'=>$post['id']])->find();
        //拼接文件路径
        $fileurl = './two_code/'.$name['name'].'.png';
        if(\db('qrcode')->delete($post['id'])){
            unlink($fileurl);
            echo json_encode(
                [
                    'id'=>$post['id'],
                    'sta'=>1
                ]
            );
        }else{
            echo '删除失败';
        }
    }
    //二维码搜索
    public function ser()
    {
        $post = request();
        $post = $post->post();
        //管理员id
        $id = $_SESSION['think']['ADMIN_ID'];
        //模糊匹配
        $where= array('like','%'.$post['text'].'%');
        $data = \db('qrcode')->where(['des'=>$where,'aid'=>$id])->select();
        if($data){
            echo json_encode($data);
        }else{
            echo '没有找到任何内容';
        }
    }
}